package com.yugong.controller;

import com.github.pagehelper.PageInfo;
import com.yugong.common.Result;
import com.yugong.entity.vo.request.AccountRequestVO;
import com.yugong.entity.vo.response.AccountListResponseVO;
import com.yugong.service.AccountService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员控制器类
 *
 * @author yugong
 * @date 2024/2/18
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Resource
    private AccountService accountService;

    /**
     * 根据账号查找用户信息
     *
     * @param pageNum   当前页
     * @param pageSize  当前页数据个数
     * @param accountNo 账号
     * @param request   用户请求
     * @return 用户信息
     */
    @GetMapping("/find-account-page")
    public Result findAccountPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  @RequestParam String accountNo,
                                  HttpServletRequest request) {
        PageInfo<AccountListResponseVO> accountListResponseVOPageInfo
                = accountService.findAccount(pageNum, pageSize, accountNo, request);
        return Result.success(accountListResponseVOPageInfo);
    }

    /**
     * 新增账户
     *
     * @param accountRequestVO 账户请求实体类
     * @return 数据库影响行数
     */
    @PostMapping("/add-account")
    public Result addAccount(@RequestBody AccountRequestVO accountRequestVO) {
        int addFlag = accountService.addAccount(accountRequestVO);
        if (0 != addFlag) {
            return Result.success(addFlag);
        } else {
            return Result.error(addFlag, "新增失败");
        }
    }

    /**
     * 删除账户
     *
     * @param accountNo 账号
     * @return 数据库影响行数
     */
    @DeleteMapping("/delete-account/{accountNo}")
    public Result deleteAccount(@PathVariable String accountNo) {
        int deleteFlag = accountService.deleteAccount(accountNo);
        if (0 != deleteFlag) {
            return Result.success(deleteFlag);
        } else {
            return Result.error(deleteFlag, "删除失败");
        }
    }

    /**
     * 查询管理员个人信息
     *
     * @param accountNo 账号
     * @return 管理员个人信息
     */
    @GetMapping("/admin-info/{accountNo}")
    public Result adminInfo(@PathVariable String accountNo) {
        AccountListResponseVO accountListResponseVO = accountService.adminInfo(accountNo);
        System.out.println(accountListResponseVO);
        return Result.success(accountListResponseVO);
    }

    /**
     * 修改个人信息
     *
     * @param accountRequestVO 账户请求实体类
     * @return 响应码
     */
    @PutMapping("/update-info")
    public Result updateInfo(@RequestBody AccountRequestVO accountRequestVO) {
        int updateFlag = accountService.updateInfo(accountRequestVO);
        return Result.success(updateFlag);
    }
}
